package com.ficus.road.maintain.third.zh.roadice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.RoadIce;
import com.ficus.road.maintain.core.model.zh.roadice.RoadIceZH;
import com.ficus.road.maintain.dao.RoadIceDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/3/31 下午8:31
 */
@Component
@Slf4j
public class ZHRoadIceInspection extends BaseZH {

    @Autowired
    private RoadIceDao roadIceDao;

    @PostConstruct
    void init() {
        arg0 = "roadIceInfo";
        arg1 = "{}";
        errMsg = "调用中海接口获取路面结冰监测点异常!";
        successMsg = "调用中海接口获取路面结冰监测点成功!";
    }

    @Scheduled(cron = "0 0/30 * * * ?", zone = "GMT+8:00")
//    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    public void refreshRoadIceInspection() {
        String roadIceInspectionStr = getZHData();
        if (StringUtils.isEmpty(roadIceInspectionStr)) {
            return;
        }
        Date today = new Date();
        List<RoadIceZH> roadIceZHList = JSONArray.parseArray(roadIceInspectionStr, RoadIceZH.class);
        log.info("路面结冰数据:"+ JSONObject.toJSONString(roadIceZHList));
        roadIceZHList.forEach(zh -> {
            String time = MyDateUtil.dateFormatStr(MyDateUtil.strParseDate(zh.getDatatime(), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            String updateTime = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd HH:mm:ss");
            RoadIce roadIce = zh.convert2RoadIce(zh, time, updateTime);
            roadIceDao.saveOrUpdate(roadIce);
        });
    }

    public static void main(String[] args) {
        String jsonStr="[{\"datatime\":\"2024-12-18T09:04:41\",\"deviceid\":\"SHA14RFM0000020\",\"devicename\":\"华洲路（迎宾下穿通道）\",\"lat\":\"31.19059951\",\"lon\":\"121.7717339\",\"runningstate\":\"11.1\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:24:52\",\"deviceid\":\"SHA14RFM0000021\",\"devicename\":\"航城路（G1501下穿通道）\",\"lat\":\"31.15675801\",\"lon\":\"121.7657427\",\"runningstate\":\"12.6\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:06:07\",\"deviceid\":\"SHA14RFM0000032\",\"devicename\":\"S20外环高速（杨高南路立交）\",\"lat\":\"31.13851377\",\"lon\":\"121.5365518\",\"runningstate\":\"9.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\"},{\"datatime\":\"2024-12-18T09:11:53\",\"deviceid\":\"SHA14RFM0000017\",\"devicename\":\"S20外环高速（康悟路）\",\"lat\":\"31.18853564\",\"lon\":\"121.7663458\",\"runningstate\":\"12.9\",\"runningstate2\":\"0\",\"runningstate3\":\"0\"},{\"datatime\":\"2024-12-18T09:06:07\",\"deviceid\":\"SHA14RFM0000033\",\"devicename\":\"S20外环高速（上南路跨线桥）\",\"lat\":\"31.14455614\",\"lon\":\"121.5674663\",\"runningstate\":\"9.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:16\",\"deviceid\":\"SHA14RFM0000034\",\"devicename\":\"中环申江路立交监测点\",\"lat\":\"31.1830569\",\"lon\":\"121.6232313\",\"runningstate\":\"13.4\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:11\",\"deviceid\":\"SHA14RFM0000035\",\"devicename\":\"中环济阳路立交监测点\",\"lat\":\"31.14810109\",\"lon\":\"121.4877668\",\"runningstate\":\"12.5\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:05\",\"deviceid\":\"SHA14RFM0000036\",\"devicename\":\"中环杨高监测点\",\"lat\":\"31.15354834\",\"lon\":\"121.5274257\",\"runningstate\":\"10.9\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:26\",\"deviceid\":\"SHA14RFM0000037\",\"devicename\":\"华夏高架环东立交监测点\",\"lat\":\"31.18935769\",\"lon\":\"121.6455215\",\"runningstate\":\"9.3\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:21\",\"deviceid\":\"SHA14RFM0000038\",\"devicename\":\"S1华夏高架立交监测点\",\"lat\":\"31.18818357\",\"lon\":\"121.7719687\",\"runningstate\":\"11.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"}]";
        List<RoadIceZH> roadIceZHList = JSONArray.parseArray(jsonStr, RoadIceZH.class);
        System.out.println(roadIceZHList.size());
        Date today = new Date();
        roadIceZHList.forEach(zh -> {
            String time = MyDateUtil.dateFormatStr(MyDateUtil.strParseDate(zh.getDatatime(), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            String updateTime = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd HH:mm:ss");
            System.out.println(JSONObject.toJSONString(zh));
            RoadIce roadIce = zh.convert2RoadIce(zh, time, updateTime);
            //roadIceDao.saveOrUpdate(roadIce);
        });
    }
}
